var app = getApp(), api = require("../../api.js"), QQMapWX = require("../../utils/qqmap-wx-jssdk.js");
var qqmapsdk;
var images = require('../../utils/images.js');
Page({
    data: {
      setTop: false,
      checkTop: 0,
      info: {
        lng: '',
        lat: '',
        address: '',
        imgs: []
      },
      themeContent: '',
      preview: [],
      uploadIcon: images.common.addImageIcon,
      positionIcon: images.nearbyShop.positionIcon,
    },
    onLoad: function(n) {
      var that = this;
      qqmapsdk = new QQMapWX({
        key: 'XALBZ-UOZCI-PHQG7-5CBUT-JH6HQ-GWFES'
      });
      wx.showLoading({
        title: "加载中"
      }), wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.userLocation']) {
            wx.authorize({
              scope: 'scope.userLocation',
              success: function (res) {
                that.getMyLocation();
              },
              complete: function () {
                wx.hideLoading();
              }
            });
          } else {
            that.getMyLocation();
          }
        }
      });
      this.getFeeItems();
    },
    getMyLocation: function(){
      var that = this;
      wx.getLocation({
        success: function (t) {
          (that.data.info.lng = t.longitude, that.data.info.lat = t.latitude) && that.setData({
            info: that.data.info
          });
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: t.latitude,
              longitude: t.longitude
            },
            success: function (addressRes) {
              var address = addressRes.result.formatted_addresses.recommend;
              that.data.info.address = address, that.setData({
                info: that.data.info
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '获取位置失败'
              });
            },
            complete: function(){
              wx.hideLoading();
            }
          });
        }
      });
    },
    goto: function(a) {
        var that = this;
        wx.getSetting({
            success: function(t) {
                t.authSetting["scope.userLocation"] ? that.location(a) : app.getauth({
                    content: "需要获取您的地理位置授权，请到小程序设置中打开授权！",
                    cancel: !1,
                    success: function(t) {
                        t.authSetting["scope.userLocation"] && that.location(a);
                    }
                });
            }
        });
    },
    location: function(t) {
        var that = this;
        wx.chooseLocation({
            success: function(res){
              (that.data.info.lng = res.longitude, that.data.info.lat = res.latitude, that.data.info.address = res.name) && that.setData({
                info: that.data.info
              });
            },
            fail: function(res){
              wx.showToast({
                title: '选择位置失败'
              })
            }
        });
    },
    uploadImg: function () {
      var t = this;
      var l = t.data.info.imgs.length;
      if(l < 6){
        app.uploader.upload({
          start: function (a) {
            wx.showLoading({
              title: "正在上传",
              mask: !0
            })
          },
          success: function (a) {
            0 == a.code ? (t.data.info.imgs.push(a.data.url), t.setData({
              info: t.data.info
            })) : wx.showToast({
              title: a.msg
            });
            console.log(t.data.info);
          },
          error: function (a) {
            console.log(a), wx.showToast({
              title: a
            })
          },
          complete: function () {
            wx.hideLoading()
          }
        });
      }else{
        wx.showToast({
          title: '最多添加6张',
          image: '/images/icon-warning.png'
        });
      }
    },
    editImg: function(e){
      var that = this;
      var i = e.currentTarget.dataset.index;
      wx.showActionSheet({
        itemList: ['删除','取消'],
        success: function(res){
          if (res.tapIndex == 0){
            wx.showToast({
              title: '删除成功'
            }),that.data.info.imgs.splice(i,1),that.setData({
              info: that.data.info
            });
          }
        }
      })
    },
    changeSet: function(e){
      var that = this;
      that.setData({
        setTop: !that.data.setTop
      });
      if (that.data.setTop){
        that.setData({
          checkTop: that.data.preview[0].day
        });
      } else {
        that.setData({
          checkTop: 0
        });
      }
    },
    checkSet: function(e){
      var that = this;
      var id = e.currentTarget.dataset.id;
      that.setData({
        checkTop: id
      })
    },
    getContent (e) {
      this.setData({themeContent: e.detail.value});
    },
    getFeeItems () {
      var that = this;
      wx.showLoading({
        title: "加载中"
      })
      app.request({
        url: api.folk.preview,
        success: function (resdata) {
          if (resdata.code == 0) {
            console.log(resdata);
            that.setData({ preview: resdata.data.top_price });
          }
        },
        complete: function () {
          wx.hideLoading();
        }
      });
    },
    confirmPublish () {
      var that = this;
      var data = {
        content: this.data.themeContent,
        lng: this.data.info.lng,
        lat: this.data.info.lat,
        location: this.data.info.address
      }
      this.data.info.imgs.map((item, index) => {
        data['pic' + index] = item;
      });
      if (this.data.setTop) {
        data.top_day = this.data.checkTop
      }
      wx.showLoading({
        title: "加载中"
      })
      app.request({
        url: api.folk.publish,
        method: 'POST',
        data: data,
        success: function (res) {
          if (res.code == 0) {
            if (res.data.total_price > 0) {
              that.payforPubulish(res.data.id);
            } else {
              wx.showToast({
                title: '提交成功！',
                image: '/images/icon-check.png'
              });
              wx.navigateTo({
                url: '/pages/folks/folks'
              })
            }
          } else {
            wx.showModal({
              content: res.msg
            });
          }
        },
        fail: function (err) {
          wx.showToast({
            title: '提交失败',
            image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
          });
        },
        complete: function (d) {
          wx.hideLoading();
        }
      })
    },
    payforPubulish (id, price) {
      var that = this;
      var data = {
        folks_id: id
      }
      wx.showLoading({
        title: "加载中"
      })
      app.request({
        url: api.folk.paydata,
        data: data,
        success: function (res) {
          if (res.code == 0) {
           wx.requestPayment({
             timeStamp: res.data.timeStamp,
             nonceStr: res.data.nonceStr,
             package: res.data.package,
             signType: res.data.signType,
             paySign: res.data.paySign,
             success: function(res){
              wx.showToast({
                title: '支付成功',
                image: "http://runwu100.img.guoxiaoge.cn/icon-check.png"
              });
              setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/folks/folks'
                })
              }, 500);
             },
             fail: function() {
              wx.showToast({
                title: '支付失败',
                image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
              });
             },
             complete: function() {
               // complete
             }
           })
          } else {
            wx.showModal({
              content: res.msg
            });
          }
        },
        fail: function (err) {
          wx.showToast({
            title: '提交失败',
            image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
          });
        },
        complete: function (d) {
          wx.hideLoading();
        }
      })
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});